/*     */ package com.newcrm.webapi;
/*     */ 
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletContext;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.servlet.http.HttpSession;
/*     */ import net.sf.json.JSONObject;
/*     */ 
/*     */ public abstract class AbstractAction
/*     */ {
/*  28 */   protected AbstractHttpServlet servlet = null;
/*     */ 
/*     */   public AbstractAction() {
/*     */   }
/*     */ 
/*     */   public AbstractAction(AbstractHttpServlet servlet) {
/*  34 */     this.servlet = servlet;
/*     */   }
/*     */ 
/*     */   protected User getLoginUser(HttpServletRequest request)
/*     */   {
/*  44 */     HttpSession session = request.getSession(true);
/*  45 */     return (User)session.getAttribute("user");
/*     */   }
/*     */ 
/*     */   protected CompanyInfo getCompany(int companyId)
/*     */   {
/*  56 */     Map companys = (Map)this.servlet
/*  57 */       .getServletContext().getAttribute("companys");
/*  58 */     if ((companys == null) || (companys.size() == 0)) {
/*  59 */       return null;
/*     */     }
/*  61 */     return (CompanyInfo)companys.get(Integer.valueOf(companyId));
/*     */   }
/*     */ 
/*     */   protected CompanyInfo getCompany(HttpServletRequest request)
/*     */   {
/*  71 */     User u = getLoginUser(request);
/*  72 */     if (u == null) {
/*  73 */       return null;
/*     */     }
/*  75 */     return getCompany(u.companyId);
/*     */   }
/*     */ 
/*     */   protected User getUser(HttpServletRequest request, String userId)
/*     */   {
/*  86 */     CompanyInfo companyInfo = getCompany(request);
/*  87 */     for (User u : companyInfo.users) {
/*  88 */       if (userId.equals(u.userId)) {
/*  89 */         return u;
/*     */       }
/*     */     }
/*     */ 
/*  93 */     return null;
/*     */   }
/*     */ 
/*     */   protected List<String> getGroupUsers(int companyId, int groupId)
/*     */   {
/* 104 */     CompanyInfo company = getCompany(companyId);
/* 105 */     return company.getGroupUsersId(groupId);
/*     */   }
/*     */ 
/*     */   protected int getUserGroupId(int companyId, String userId)
/*     */   {
/* 116 */     CompanyInfo company = getCompany(companyId);
/*     */ 
/* 118 */     for (User u : company.users) {
/* 119 */       if (u.userId.equals(userId)) {
/* 120 */         return u.groupId;
/*     */       }
/*     */     }
/* 123 */     return 0;
/*     */   }
/*     */ 
/*     */   protected List<String> getLeaders(HttpServletRequest request)
/*     */   {
/* 132 */     User u = getLoginUser(request);
/* 133 */     int groupId = u.groupId;
/*     */ 
/* 136 */     List leaders = new ArrayList();
/*     */ 
/* 138 */     for (User user : getCompany(request).users)
/*     */     {
/* 140 */       if (!user.userId.equals(u.userId))
/*     */       {
/* 143 */         if (user.isAdmin) {
/* 144 */           leaders.add(user.userId);
/*     */         }
/* 148 */         else if ((user.isGroupAdmin) && (user.groupId == groupId)) {
/* 149 */           leaders.add(user.userId);
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 154 */     return leaders;
/*     */   }
/*     */ 
/*     */   protected List<String> getGroupUsers(HttpServletRequest request)
/*     */   {
/* 164 */     User u = getLoginUser(request);
/* 165 */     CompanyInfo company = getCompany(request);
/* 166 */     return company.getGroupUsersId(u.groupId);
/*     */   }
/*     */ 
/*     */   protected void showErrorMessage(HttpServletResponse response, String msg)
/*     */     throws IOException
/*     */   {
/* 178 */     response.setContentType("text/html;charset=utf-8");
/* 179 */     PrintWriter out = response.getWriter();
/* 180 */     JSONObject jsonObject = new JSONObject();
/* 181 */     jsonObject.put("error", "1");
/* 182 */     jsonObject.put("msg", msg);
/* 183 */     out.println(jsonObject);
/* 184 */     out.flush();
/* 185 */     out.close();
/*     */   }
/*     */ 
/*     */   protected void showSuccessMessage(HttpServletResponse response)
/*     */     throws IOException
/*     */   {
/* 196 */     response.setContentType("text/html;charset=utf-8");
/* 197 */     PrintWriter out = response.getWriter();
/* 198 */     JSONObject jsonObject = new JSONObject();
/* 199 */     jsonObject.put("error", "0");
/* 200 */     out.println(jsonObject);
/* 201 */     out.flush();
/* 202 */     out.close();
/*     */   }
/*     */ 
/*     */   public abstract void parse(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
/*     */     throws IOException, ServletException, NotFoundException;
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.AbstractAction
 * JD-Core Version:    0.6.1
 */